//
//  SettleListTableViewCell.swift
//  kxf
//
//  Created by Mac on 2024/3/19.
//

import Foundation
import UIKit

class SettleListTableViewCell: UITableViewCell {
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = .clear
        self.selectionStyle = .none
        initUI()
    }
    lazy var whiteView : UIView = {
        let vi = UIView()
        vi.isUserInteractionEnabled = true
        vi.layer.cornerRadius = kFitWidth(8)
        vi.clipsToBounds = true
        vi.backgroundColor = .white
        
        return vi
    }()
    lazy var payTypeIcon : UIImageView = {
        let img = UIImageView()
        img.contentMode = .scaleAspectFit
        
        return img
    }()
    lazy var nameLabel : UILabel = {
        let lab = UILabel()
        lab.textColor = .black
        lab.font = .systemFont(ofSize: 16, weight: .medium)
        lab.adjustsFontSizeToFitWidth = true
        
        return lab
    }()
    lazy var statusLabel : UILabel = {
        let lab = UILabel()
        lab.textColor = .COLOR_TEXT_BLACK666
        lab.font = .systemFont(ofSize: 14, weight: .regular)
        
        return lab
    }()
    lazy var timeLabel : UILabel = {
        let lab = UILabel()
        lab.textColor = .COLOR_TEXT_BLACK666
        lab.font = .systemFont(ofSize: 12, weight: .regular)
        
        return lab
    }()
    lazy var moneyLabel : UILabel = {
        let lab = UILabel()
        lab.textColor = .black
        lab.font = .systemFont(ofSize: 18, weight: .bold)
        lab.adjustsFontSizeToFitWidth = true
        lab.textAlignment = .right
        
        return lab
    }()
}

extension SettleListTableViewCell{
    func updateUI(dict:NSDictionary) {
        if dict["payType"]as? Int ?? 0 == 2 {//微信
            payTypeIcon.setImgLocal(imgName: "pay_type_wechat_icon")
        }else if dict["payType"]as? Int ?? 0 == 3 {//支付宝
            payTypeIcon.setImgLocal(imgName: "pay_type_alipay_icon")
        }else if dict["channel"]as? String ?? "" == "首信易钱包"{
            payTypeIcon.setImgLocal(imgName: "pay_type_sxy_icon")
        }else{
            payTypeIcon.setImgLocal(imgName: "pay_type_unipay_icon")
        }
        timeLabel.text = "\(Date().timestampToDate(timestamp: TimeInterval((dict["createTime"]as? Int ?? 0)/1000)))"
        moneyLabel.text = "¥\(dict["amount"]as? Float ?? 0.00)"
        
        if dict["userPayOrderStatus"]as? Int ?? -1 == 0 {
            statusLabel.text = "交易成功"
        }else if dict["userPayOrderStatus"]as? Int ?? -1 == 3{
            statusLabel.text = "未支付"
        }else{
            statusLabel.text = ""
        }
    }
    func updateUIForTrade(dict:NSDictionary) {
        if dict["payType"]as? Int ?? 0 == 2 {//微信
            payTypeIcon.setImgLocal(imgName: "pay_type_wechat_icon")
        }else if dict["payType"]as? Int ?? 0 == 3 {//支付宝
            payTypeIcon.setImgLocal(imgName: "pay_type_alipay_icon")
        }else if dict["channel"]as? String ?? "" == "首信易钱包"{
            payTypeIcon.setImgLocal(imgName: "pay_type_sxy_icon")
        }else{
            payTypeIcon.setImgLocal(imgName: "pay_type_unipay_icon")
        }
//        nameLabel.text = "\(dict["username"]as? String ?? "")"
        timeLabel.text = "时间：\(Date().timestampToDate(timestamp: TimeInterval((dict["createTime"]as? Int ?? 0)/1000)))"
        moneyLabel.text = "¥\(WHUtils.convertStringToString("\(dict["amount"]as? Double ?? 0.00)") ?? "0")"
        
//        if dict["userPayOrderStatus"]as? Int ?? -1 == 0 {
//            nameLabel.text = "交易成功"
//        }else if dict["userPayOrderStatus"]as? Int ?? -1 == 3{
//            nameLabel.text = "未支付"
//        }else if dict["userPayOrderStatus"]as? Int ?? -1 == 5{
//            nameLabel.text = "退款成功"
//        }else{
//            nameLabel.text = "\(dict["username"]as? String ?? "")"
//        }
        if dict["userPayOrderStatus"]as? Int ?? -1 == 0 {
            nameLabel.text = "交易成功"
        }else if dict["userPayOrderStatus"]as? Int ?? -1 == 1{
            nameLabel.text = "交易失败"
        }else if dict["userPayOrderStatus"]as? Int ?? -1 == 2{
            nameLabel.text = "未支付"
        }else if dict["userPayOrderStatus"]as? Int ?? -1 == 3{
            nameLabel.text = "交易中"
        }else if dict["userPayOrderStatus"]as? Int ?? -1 == 4{
            nameLabel.text = "退款中"
        }else if dict["userPayOrderStatus"]as? Int ?? -1 == 5{
            nameLabel.text = "退款成功"
        }else if dict["userPayOrderStatus"]as? Int ?? -1 == 6{
            nameLabel.text = "退款失败"
        }
    }
}

extension SettleListTableViewCell{
    func initUI() {
        contentView.addSubview(whiteView)
        whiteView.addSubview(payTypeIcon)
        whiteView.addSubview(nameLabel)
        whiteView.addSubview(statusLabel)
        whiteView.addSubview(timeLabel)
        whiteView.addSubview(moneyLabel)
        
        setConstrait()
    }
    func setConstrait() {
        whiteView.snp.makeConstraints { make in
            make.centerX.lessThanOrEqualToSuperview()
            make.width.equalTo(kFitWidth(343))
            make.height.equalTo(kFitWidth(84))
            make.top.equalToSuperview()
        }
        payTypeIcon.snp.makeConstraints { make in
            make.left.top.equalTo(kFitWidth(12))
            make.width.height.equalTo(kFitWidth(22))
        }
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(kFitWidth(41))
            make.centerY.lessThanOrEqualTo(payTypeIcon)
            make.right.equalTo(kFitWidth(-20))
        }
        statusLabel.snp.makeConstraints { make in
            make.left.equalTo(kFitWidth(12))
            make.top.equalTo(kFitWidth(45))
        }
        timeLabel.snp.makeConstraints { make in
            make.left.equalTo(statusLabel.snp.right).offset(kFitWidth(4))
            make.top.equalTo(kFitWidth(46))
        }
        moneyLabel.snp.makeConstraints { make in
            make.centerY.lessThanOrEqualToSuperview()
            make.right.equalTo(kFitWidth(-12))
            make.width.equalTo(kFitWidth(120))
        }
    }
}
